function loadImage(...path) {
    return new Promise((resolve) => {
        path.forEach((i) => {
            if (!PIXI.loader.resources.hasOwnProperty(i)) {
                PIXI.loader.add(i, { crossOrigin: true });
            }
        });
        PIXI.loader.load(() => {
            resolve();
        });
    });
}
class Progress {
    constructor() {
        const style = {
            fontFamily: "DOUYU Font",
            fontSize: 24 * px,
            fill: "white",
        };
        this.message = new PIXI.Text("加载文件中", style);
        app.stage.addChild(this.message);
        this.goPosition();
    }
    changeText(text) {
        this.message.text = text;
        this.goPosition();
    }
    goPosition() {
        this.message.y = app.config.height - this.message.height - 72 * px;
        this.message.x = app.config.width / 2 - this.message.width / 2;
    }
    destroy() {
        app.stage.removeChild(this.message);
    }
}

import Transaction from "https://cdn.jsdelivr.net/npm/@konghayao/promise-transaction/dist/promiseTransaction.browser.js";
window.assetsLoader = async function (Resources) {
    const message = new Progress();
    const entries = Object.entries(Resources);
    const tra = entries.map(([name, path], index) => {
        return [
            name,
            async () => {
                const cent =
                    Math.ceil(((index + 1) * 100) / entries.length) + " %";
                console.log("加载资源", cent, name, path);
                let result;
                if (path instanceof Array) {
                    // 当为数组时，保持数组形式返回，表示一个状态
                    await loadImage(...path);
                    result = path.map((i) => PIXI.loader.resources[i].texture);
                } else if (path instanceof Object) {
                    // 当为对象时，保存为一个数组，但是数组属性上有 key 对应的 资源的 index
                    result = [];
                    result.states = {};
                    const entries = [...Object.entries(path)];
                    await loadImage(...entries.map((i) => i[1]).flat());

                    entries.reduce((last, [key, value], index) => {
                        const resource = value.map(
                            (i) => PIXI.loader.resources[i].texture
                        );
                        result.push(...resource);
                        result.states[key] = resource;
                        return last + resource.length;
                    }, 0);
                } else {
                    await loadImage(path);
                    result = PIXI.loader.resources[path].texture;
                }
                message.changeText(`${cent} 加载 ${name} 完成`);
                return result;
            },
        ];
    });
    return new Transaction(tra).run().then((map) => {
        window.Resources = {
            ...window.Resources,
            ...Object.fromEntries(map.entries()),
        };
        message.destroy();

        return map;
    });
};
